package lms.dwr.demo.web.action.student;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import lms.dwr.demo.common.Constants;
import lms.dwr.demo.common.ServiceException;
import lms.dwr.demo.model.ModelDistrict;
import lms.dwr.demo.model.ModelStudent;
import lms.dwr.demo.service.ServiceDistrict;
import lms.dwr.demo.service.ServiceStudent;
import lms.dwr.demo.utils.StringUtils;
import lms.dwr.demo.web.action.BaseAction;

public class StudentAction
extends BaseAction
{
	
	/**
	 * The LOGGER
	 */
	private static final Logger LOGGER = Logger.getLogger(StudentAction.class);
	
	/**
	 * The student service
	 */
	private ServiceStudent serviceStudent;
	
	/**
	 * The district service
	 */
	private ServiceDistrict serviceDistrict;
	
	/**
	 * The student index page
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward studentPageIndex(ActionMapping mapping, 
		     ActionForm form, 
		     HttpServletRequest request,
		     HttpServletResponse response)
	{
		try
		{
			int offset = request.getParameter(Constants.DefaultPageParamFlag) == null ? 0 : 
				(Integer.parseInt(request.getParameter(Constants.DefaultPageParamFlag)) - 1) * Constants.DefaultPageSize;
			
			request.setAttribute("student.page", 
					this.serviceStudent.getStudentPagination(null, Constants.DefaultPageSize, offset, null, null));
			
			return mapping.findForward("admin.student.page.index");
		}
		catch (ServiceException e)
		{
			LOGGER.error("It failed to render the student index page!", e);
		}
		
		return mapping.findForward("error"); 
	}
	
	/**
	 * The student edit page
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward studentPageEdit(ActionMapping mapping, 
		     ActionForm form, 
		     HttpServletRequest request,
		     HttpServletResponse response)
	{
		String id = request.getParameter("id");
		if (StringUtils.isNotEmpty(id))
		{
			try
			{
				ModelStudent entity = this.serviceStudent.getStudentById(id);
				if (entity != null)
				{
					request.setAttribute("entity", entity);
					request.setAttribute("districts", this.serviceDistrict.getDistrictByEntity(new ModelDistrict()));
					return mapping.findForward("admin.student.page.edit");
				}
				else
				{
					LOGGER.debug("The student entity with id(" + id + ") does not exist.");
				}
			} 
			catch (ServiceException e)
			{
				LOGGER.error("It failed to fetch the student entity with id: " + id, e);
			}
			
		}
		
		return mapping.findForward("error"); 
	}
	
	public static Logger getLogger()
	{
		return LOGGER;
	}

	public void setServiceStudent(ServiceStudent serviceStudent)
	{
		this.serviceStudent = serviceStudent;
	}

	public ServiceStudent getServiceStudent()
	{
		return serviceStudent;
	}
	
	public ServiceDistrict getServiceDistrict() 
	{
		return serviceDistrict;
	}

	public void setServiceDistrict(ServiceDistrict serviceDistrict) 
	{
		this.serviceDistrict = serviceDistrict;
	}
}
